package log;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Log {
    public static final String black = "30";
    public static final String red = "31";
    public static final String green = "32";
    public static final String yellow = "33";
    public static final String blue = "34";
    public static final String purple = "35";
    public static final String green_blue = "36";
    public static final String white = "37";
//  背景颜色(40黑 41红 42绿 43黄 44蓝 45紫 46青 47灰)

    Class<?> clazz;
    private boolean enable = false;//是否启用日志
    private boolean colorMode = false;//控制台颜色
    private String prifex;//前缀

    public Log(Class<?> clazz) {
        this.clazz = clazz;
    }

    public Log(String prifex) {
        this.prifex = prifex;
    }

    public static void printIndent(String msg, String color,String bg) {
        System.out.print("\033[" + bg + ";" + color + "m" + msg + "\033[0m");
    }

    public static void printName(String msg, String color) {
        System.out.print("\033[1;" + color + "m" + msg  + "\033[0m");

    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public boolean isColorMode() {
        return colorMode;
    }

    public void setColorMode(boolean colorMode) {
        this.colorMode = colorMode;
    }

    public Class<?> getClazz() {
        return clazz;
    }

    public void setClazz(Class<?> clazz) {
        this.clazz = clazz;
    }

    public String getPrifex() {
        return prifex;
    }

    public void setPrifex(String prifex) {
        this.prifex = prifex;
    }

    /***
     * @param msg 打印的消息 info 级别 绿色
     */
    public void i(Object msg) {
        print(0, msg, green);
    }

    public void d(Object msg) {
        print(1, msg, blue);
    }

    public void w(Object msg) {
        print(2, msg, yellow);
    }

    public void e(Object msg) {
        print(3, msg, red);
    }

    public void print(int level, Object msg, String color) {
        if (prifex == null && clazz != null) {
            prifex = clazz.getName();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss - SSS");
        String format = sdf.format(new Date());
        System.out.print("[ONLY-TEST]" + "[" + prifex + "]");
        System.out.print("\033[1;" + purple + "m " + format + "ms\033[0m ");
        System.out.println("\033[1;" + color + "m " + msg + "\033[0m \n");
    }
}
